package com.shbaiche.bifenba.ui.data;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.google.gson.Gson;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.data.CompeteGroupMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseFragment;
import com.shbaiche.bifenba.ui.base.XStateFragmentAdapter;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.match.MatchListFragment;
import com.shbaiche.bifenba.ui.my.FollowTeamFragment;
import com.shbaiche.bifenba.ui.my.LoginActivity;
import com.shbaiche.bifenba.ui.my.MessageListActivity;
import com.shbaiche.bifenba.ui.my.MyCollectionActivity;
import com.shbaiche.bifenba.ui.my.MyCommentListActivity;
import com.shbaiche.bifenba.ui.my.MyFollowActivity;
import com.shbaiche.bifenba.ui.my.SettingsActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.CircularImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xstatecontroller.XStateController;
import okhttp3.Call;

/**
 * 数据首页
 */

public class DataFragment extends BaseFragment {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.category_strip)
    CategoryTabStrip tabs;

    @BindView(R.id.contentLayout)
    XStateController contentLayout;

    @BindView(R.id.view_pager_data)
    ViewPager pager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private XStateFragmentAdapter adapter;



    public static DataFragment newInstance() {
        return new DataFragment();
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        contentLayout.loadingView(View.inflate(getActivity(), R.layout.view_loading, null));

        if (contentLayout != null) {
            contentLayout.showLoading();
        }
        loadGroup();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            //相当于Fragment的onResume，为true时，Fragment已经可见
            if(contentLayout!=null && !(adapter!=null && adapter.getCount()>0)){
                loadGroup();
            }
        } else {
            //相当于Fragment的onPause，为false时，Fragment不可见
        }
    }

    private void loadGroup(){
        contentLayout.showLoading();
        CompeteGroupMessage message = new CompeteGroupMessage();
        message.setType(CompeteGroupMessage.CompeteGroupType.DATA);
        NetApi.dataGroup(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                contentLayout.showContent();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                contentLayout.showContent();
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                contentLayout.showContent();
                CompeteGroupMessage result =new Gson().fromJson(response.getMessage(),CompeteGroupMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null && result.getVoList()!=null){
                        Log.e("t","onResponse");
                        fragmentList.clear();
                        String[] tits = new String[result.getVoList().size()];
                        for (int i = 0;i< result.getVoList().size();i++){
                            tits[i] = result.getVoList().get(i).getName();
                            result.getVoList().get(i).setStandingYear(result.getStandingYear());
                            result.getVoList().get(i).setShooterYear(result.getShooterYear());
                            result.getVoList().get(i).setTeamYear(result.getTeamYear());
                            result.getVoList().get(i).setMatchesYear(result.getMatchesYear());


                            DataContentFragment fragment = new DataContentFragment();
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("data",result.getVoList().get(i));
                            fragment.setArguments(bundle);
                            fragmentList.add(fragment);
                        }

                        adapter = new XStateFragmentAdapter(getFragmentManager(), fragmentList, tits);

                        pager.setAdapter(adapter);
                        pager.setOffscreenPageLimit(fragmentList.size());

                        tabs.setViewPager(pager);
                    }
                }else{
                    Log.e("t","onResponse getCode "+response.getCode());
                }
            }
        });
    }


    private class DoClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.my_layout_info:
                    LoginActivity.launch(context);
                    break;
                case R.id.my_btn_message:
                    MessageListActivity.launch(context);
                    break;
                case R.id.my_btn_follow:
                    MyFollowActivity.launch(context);
                    break;
                case R.id.my_btn_collection:
                    MyCollectionActivity.launch(context);
                    break;
                case R.id.my_btn_comment:
                    MyCommentListActivity.launch(context);
                    break;
                case R.id.my_btn_settings:
                    SettingsActivity.launch(context);
                    break;
            }
        }
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_data;
    }
}
